package com.ruoyi.rycc.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.rycc.core.cosUpload.CosUpload;
import com.ruoyi.rycc.domain.CcChapter;
import com.ruoyi.rycc.service.ICcChapterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 章节Controller
 *
 * @author antg
 * @date 2021-09-06
 */
@Controller
@RequestMapping("/chapter/chapter")
public class CcChapterController extends BaseController {
    private String prefix = "chapter/chapter";

    @Autowired
    private ICcChapterService ccChapterService;
    @Autowired
    private CosUpload cosUpload;

    @RequiresPermissions("chapter:chapter:view")
    @GetMapping("/{id}")
    public String chapter(@PathVariable("id") Long id, ModelMap mmap) {
        mmap.put("courseId", id);
        return prefix + "/chapter";
    }

    /**
     * 查询章节树列表
     */
    @RequiresPermissions("chapter:chapter:list")
    @PostMapping("/list/{courseId}")
    @ResponseBody
    public List<CcChapter> list(@PathVariable("courseId") Long courseId, CcChapter ccChapter) {
        startPage();
        ccChapter.setCourseId(courseId);
        List<CcChapter> list = ccChapterService.selectCcChapterList(ccChapter);
        return list;
    }

    /**
     * 导出章节列表
     */
    @RequiresPermissions("chapter:chapter:export")
    @Log(title = "章节", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CcChapter ccChapter) {
        List<CcChapter> list = ccChapterService.selectCcChapterList(ccChapter);
        ExcelUtil<CcChapter> util = new ExcelUtil<CcChapter>(CcChapter.class);
        return util.exportExcel(list, "章节数据");
    }

    /**
     * 新增章
     */
    @GetMapping(value = {"/add/{courseId}"})
    public String addChapter(@PathVariable("courseId") Long courseId, ModelMap mmap) {
        if (StringUtils.isNotNull(courseId)) {
            mmap.put("courseId", courseId);
        }
        return prefix + "/add-chapter";
    }

    /**
     * 新增节
     */
    @GetMapping(value = {"/add/{chapterId}/{courseId}"})
    public String addSection(@PathVariable("courseId") Long courseId, @PathVariable("chapterId") Long chapterId, ModelMap mmap) {
        if (StringUtils.isNotNull(courseId)) {
            mmap.put("courseId", courseId);
            mmap.put("chapterId", chapterId);
        }
        return prefix + "/add-section";
    }

    /**
     * 保存章
     */
    @RequiresPermissions("chapter:chapter:add")
    @Log(title = "章节", businessType = BusinessType.INSERT)
    @PostMapping("/addSaveChapter")
    @ResponseBody
    public AjaxResult addSaveChapter(CcChapter ccChapter) {
        //章的parentId默认0
        ccChapter.setParentId(0L);
        return toAjax(ccChapterService.insertCcChapter(ccChapter));
    }

    /**
     * 保存节
     */
    @RequiresPermissions("chapter:chapter:add")
    @Log(title = "章节", businessType = BusinessType.INSERT)
    @PostMapping("/addSaveSection")
    @ResponseBody
    public AjaxResult addSaveSection(@RequestParam(value = "sectionVideo", required = false) MultipartFile sectionVideo, CcChapter ccChapter) throws IOException {
        if (sectionVideo != null) {
            String sectionVideoPath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), sectionVideo);
            ccChapter.setSectionVideoUrl(sectionVideoPath);
        }
        return toAjax(ccChapterService.insertCcChapter(ccChapter));
    }

    /**
     * 修改章节
     */
    @GetMapping("/edit/{id}/{courseId}")
    public String edit(@PathVariable("id") Long id, @PathVariable("courseId") Long courseId, ModelMap mmap) {
        CcChapter ccChapter = ccChapterService.selectCcChapterById(id, courseId);
        mmap.put("ccChapter", ccChapter);
        if (ccChapter.getParentId() == 0) {
            //章
            return prefix + "/edit-chapter";
        } else {
            //节
            return prefix + "/edit-section";
        }
    }

    /**
     * 修改保存章节
     */
    @RequiresPermissions("chapter:chapter:edit")
    @Log(title = "章节", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestParam(value = "sectionVideo", required = false) MultipartFile sectionVideo, CcChapter ccChapter) throws IOException {
        if (sectionVideo != null) {
//            String sectionVideoPath = FileUploadUtils.upload(RuoYiConfig.getUploadPath(), sectionVideo);
            //上传到cos
            String sectionVideoPath = cosUpload.upload(sectionVideo);
            ccChapter.setSectionVideoUrl(sectionVideoPath);
        }
        return toAjax(ccChapterService.updateCcChapter(ccChapter));
    }

    /**
     * 删除
     */
    @RequiresPermissions("chapter:chapter:remove")
    @Log(title = "章节", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{id}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("id") Long id) {
        return toAjax(ccChapterService.deleteCcChapterById(id));
    }

    /**
     * 选择章节树
     */
    @GetMapping(value = {"/selectChapterTree/{id}/{courseId}", "/selectChapterTree/{courseId}"})
    public String selectChapterTree(@PathVariable(value = "id", required = false) Long id, @PathVariable(value = "courseId", required = false) Long courseId, ModelMap mmap) {

        if (StringUtils.isNotNull(id)) {
            mmap.put("ccChapter", ccChapterService.selectCcChapterById(id, courseId));
        }
        return prefix + "/tree";
    }

    /**
     * 加载章节树列表
     */
    @GetMapping("/treeData")
    @ResponseBody
    public List<Ztree> treeData() {
        List<Ztree> ztrees = ccChapterService.selectCcChapterTree();
        return ztrees;
    }
}
